﻿using System;
using System.Diagnostics;
using log4net;

namespace Sharp.Infrastructure.Crosscutting
{
    /// <summary>
    /// 日志帮助类
    /// </summary>
    /// <remarks>日志使用注意事项：记录跟踪信息如果数据量很大，则优先级设置为最低。以便在生产环境下设置日志过滤级别。</remarks>
    [DebuggerStepThrough]
    public class LogHelper
    {
        static LogHelper()
        {
            LoadUnhandledException();
        }

        #region 变量定义

        //ILog对象
        private static ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


        #endregion

        public delegate void LogRecordHandler(object sender, EventArgs e);

        public static event LogRecordHandler OnLogRecorded;


        #region 定义信息二次处理方式
        private static void HandMessage(object msg)
        {
            if (OnLogRecorded != null)
            {
                OnLogRecorded.Invoke(msg, null);

            }
        }
        private static void HandMessage(object msg, Exception ex)
        {
            if (OnLogRecorded != null)
            {
                OnLogRecorded.Invoke(ex, null);
            }
        }
        private static void HandMessage(string format, params object[] args)
        {
            if (OnLogRecorded != null)
            {
                OnLogRecorded.Invoke(string.Format(format, args), null);
            }
        }
        #endregion

        #region 封装Log4net
        public static void Debug(object message)
        {
            HandMessage(message);
            if (Log.IsDebugEnabled)
            {
                Log.Debug(message);
            }
        }
        public static void Debug(object message, Exception ex)
        {
            HandMessage(message, ex);
            if (Log.IsDebugEnabled)
            {
                Log.Debug(message, ex);
            }
        }
        public static void DebugFormat(string format, params object[] args)
        {
            HandMessage(format, args);
            if (Log.IsDebugEnabled)
            {
                Log.DebugFormat(format, args);
            }
        }
        public static void Error(object message)
        {
            HandMessage(message);
            if (Log.IsErrorEnabled)
            {
                Log.Error(message);
            }
        }
        public static void Error(object message, Exception ex)
        {
            HandMessage(message, ex);
            if (Log.IsErrorEnabled)
            {
                Log.Error(message, ex);
            }
        }
        public static void ErrorFormat(string format, params object[] args)
        {
            HandMessage(format, args);
            if (Log.IsErrorEnabled)
            {
                Log.ErrorFormat(format, args);
            }
        }
        public static void Fatal(object message)
        {
            HandMessage(message);
            if (Log.IsFatalEnabled)
            {
                Log.Fatal(message);
            }
        }
        public static void Fatal(object message, Exception ex)
        {
            HandMessage(message, ex);
            if (Log.IsFatalEnabled)
            {
                Log.Fatal(message, ex);
            }
        }
        public static void FatalFormat(string format, params object[] args)
        {
            HandMessage(format, args);
            if (Log.IsFatalEnabled)
            {
                Log.FatalFormat(format, args);
            }
        }
        public static void Info(object message)
        {
            HandMessage(message);
            if (Log.IsInfoEnabled)
            {
                Log.Info(message);
            }
        }
        public static void Info(object message, Exception ex)
        {
            HandMessage(message, ex);
            if (Log.IsInfoEnabled)
            {
                Log.Info(message, ex);
            }
        }
        public static void InfoFormat(string format, params object[] args)
        {
            HandMessage(format, args);
            if (Log.IsInfoEnabled)
            {
                Log.InfoFormat(format, args);
            }
        }
        public static void Warn(object message)
        {
            HandMessage(message);
            if (Log.IsWarnEnabled)
            {
                Log.Warn(message);
            }
        }
        public static void Warn(object message, Exception ex)
        {
            HandMessage(message, ex);
            if (Log.IsWarnEnabled)
            {
                Log.Warn(message, ex);
            }
        }
        public static void WarnFormat(string format, params object[] args)
        {
            HandMessage(format, args);
            if (Log.IsWarnEnabled)
            {
                Log.WarnFormat(format, args);
            }
        }
        #endregion



        #region 定义常规应用程序中未处理的异常信息记录方式
        private static void LoadUnhandledException()
        {
            AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
            {
                Log.Fatal("未处理的异常", e.ExceptionObject as Exception);
            };
        }
        #endregion
    }
}